package algebra.rewriting.transform;

//import entities.concept.AndConcept;
import entities.concept.AndConcept;
import algebra.op.AlgebraicOP;
import algebra.op.SelectionOP;
import algebra.rewriting.RewriteRule;

public class MergeSelection extends RewriteRule {

	private final AlgebraicOP op;

	// if decomp, c and d -> two selections, else the reverse op
	public MergeSelection(AlgebraicOP op) {
		this.op = op;
	}

	@Override
	public AlgebraicOP rewrite() {
		SelectionOP p1 = op.asSelectionOP();
		SelectionOP p2 = p1.getSubquery().asSelectionOP();
		return new SelectionOP(p2.getSubquery(), new AndConcept(
				p1.getSelectionCondition(), p2.getSelectionCondition()),
				p1.getPlanEvalKB());
	}

	@Override
	public boolean applicable() {
		if (op instanceof SelectionOP
				&& (op.asSelectionOP().getSubquery() instanceof SelectionOP)) {
			return true;
		}
		return false;
	}

}
